// miniprogram/pages/my/member/add.js
import *as R from 'ramda';
import config from '../../../config/index';
import WxValidate from "../../../utils/vendor/WxValidate";
import U from "../../../utils/index";
import Dialog from "../../../miniprogram_npm/vant-weapp/dialog/dialog";
const globalData = getApp().globalData;
const validator = {
  rules: {
    role: {
      required: true,
    },
    username: {
      required: true,
    },
    password: {
      required: true,
    },
    realname: {
      required: true,
    },
  },
  messages: {
    role: {
      required: '请选择用户身份',
    },
    username: {
      required: '请输入用户名',
    },
    password: {
      required: '请输入密码',
    },
    realname: {
      required: '请输入姓名',
    },
  },
}
const wxValidateCreate = new WxValidate(validator.rules, validator.messages);
const changeValidatorUpdate = R.map(R.pick([ 'password', 'realname' ]));
const wxValidateUpdate = new WxValidate(changeValidatorUpdate(validator).rules, changeValidatorUpdate(validator).messages);

const handleRequestCreate = U.reqPostShow('/user/add_user', '新增用户成功！');
const handleRequestUpdate = U.reqPostShow('/user/update', '修改用户成功！');
const handleRequestDel = U.reqPostShow('/user/delete', '删除用户成功！');
const handleRequestFind = data => U.handleRequest({}, () => U.reqPost({
  url: '/user/user_list',
  data
}))
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShowPage: true,
    // userId是不是当前登录帐户
    isOwn: false,
    role: null,
    form: {},
    roleTypes: config.roleTypes,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    R.compose(
      R.unless(
        () => R.isNil(options.userId),
        R.compose(
          R.compose(
            U.fork,
            R.map(
              res => this.setData({
                ['form.user_id']: Number(options.userId),
                ['form.realname']: res.data.list[0].realname,
                isShowPage: true,
              }),
            ),
            () => handleRequestFind({
              user_id: Number(options.userId),
              is_agent: Number(options.agent)
            })
          ),
          () => this.setData({ isShowPage: false }),
          () => wx.setNavigationBarTitle({ title: '修改用户' })
        )
      ),
      () => this.setData({
        role: globalData.userInfo.role,
        isOwn: Number(options.userId) === globalData.userInfo.userId
      }),
    )()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  setFormData(e) {
    U.setFormData(e).run(this);
  },
  onClickRole(e) {
    this.setData({
      ['form.role']: Number(e.currentTarget.dataset.name),
    })
  },
  onSubmit() {
    R.compose(
      U.runIO,
      U.handleError,
      R.map(
        R.compose(
          U.fork,
          R.map(R.compose(U.runIO, U.backReqSuc)),
          R.ifElse(
            () => R.isNil(this.data.form.user_id),
            handleRequestCreate,
            handleRequestUpdate
          )
        )
      ),
      R.ifElse(
        () => R.isNil(this.data.form.user_id),
        U.validate(wxValidateCreate),
        U.validate(wxValidateUpdate)
      ),
      R.tap(form => this.setData({ form })),
      R.map(U.trim),
    )(this.data.form);
  },
  onClickDel(e) {
    R.composeP(
      R.compose(
        U.fork,
        R.map(R.compose(U.runIO, U.backReqSuc)),
        () => handleRequestDel({ user_id: this.data.form.user_id }),
      ),
      () => Dialog.confirm({ message: '是否确认删除该用户？' })
    )();
  },
})
